package com.mcxx.modules.miniSalvation.family.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.family.dao.write.DemocraticAppraisalWriteDao;
import com.mcxx.modules.miniSalvation.family.entity.DemocraticAppraisalEntity;
import com.mcxx.modules.miniSalvation.order.dao.write.OrderCertWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.OrderDTO;
import com.mcxx.modules.miniSalvation.order.service.read.OrderCertReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.modules.miniSalvation.order.service.write.OrderWriteService;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
@TaTransactional
public class DemocraticAppraisalWriteService {
    @Autowired
    private DemocraticAppraisalWriteDao democraticAppraisalWriteDao;
    @Autowired
    private OrderCertWriteDao orderCertWriteDao;
    @Autowired
    private OrderWriteService orderWriteService;
    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private OrderCertReadService orderCertReadService;


    public int addDemocraticAppraisal(DemocraticAppraisalEntity entity, List<String> orderCertIdList) {

        entity.setId(Constant.getUUID());
        int rst = democraticAppraisalWriteDao.addDemocraticAppraisal(entity);
        if(rst <= 0){
            log.info("保存民主评议的信息 异常");
            throw new BizException("-1", "保存民主评议的信息 异常");
        }


        if(null == orderCertIdList || orderCertIdList.size() <= 0){
            return 1;
        }


        rst = orderCertWriteDao.updateById(orderCertIdList, entity.getOrderId(), "5", entity.getId());
        if(rst <= 0){
            log.info("保存民主评议的信息-更新证明材料关系 异常，无法插入更新数据");
            throw new BizException("-1", "民主评议的证明材料操作 异常");
        }
        return 1;
    }


    public void modifyDemocraticAppraisal(DemocraticAppraisalEntity entity, List<String> orderCertIdList) {

        if("2".equals(entity.getType())){
            entity.setAppraisalResult("1");
        }

        int rst = democraticAppraisalWriteDao.updateDemocraticAppraisal(entity);
        if(rst <= 0){
            log.info("orderId = {},修改民主评议失败", entity.getOrderId());
            throw new BizException("-1", "修改民主评议失败");
        }

        if(!CollectionUtils.isEmpty(orderCertIdList)){
            log.info("更新对应的证明材料关系,材料：{}", orderCertIdList);
            rst = orderCertWriteDao.updateById(orderCertIdList, entity.getOrderId(), "5", entity.getId());
            if(rst <= 0){
                log.info("保存民主评议信息-更新证明材料关系 异常，无法插入更新数据");
                throw new BizException("-1", "民主评议的证明材料操作 异常");
            }
        }

        if("2".equals(entity.getType())){

            int i = orderCertReadService.queryCertIsExiten(new ArrayList<String>() {{
                add(entity.getOrderId());
            }}, "601", "5");
            int j = orderCertReadService.queryCertIsExiten(new ArrayList<String>() {{
                add(entity.getOrderId());
            }}, "602", "5");
            if (i <= 0 || j <= 0) {
                throw new BizException("-1", "请上传民主评议证明材料");
            }


            OrderDTO orderDTO = orderReadService.getStateAndVersion(entity.getOrderId());

            orderWriteService.updateOrderState(orderDTO, orderDTO.getBizState(), Constant.BizStateNew.CONFIRM_APPROVE);
        }
    }



    public int deleteByOrderId(String orderId){
        return democraticAppraisalWriteDao.deleteByOrderId(orderId);
    }
}
